*------------------------------------------------------------------------------------------------------------%	
* Figure 4: Effect of large retailer MWs on own wages, major events
*------------------------------------------------------------------------------------------------------------%	
	* Merge with policy firm data
	use "$data/cb/stacked_policy_firm_dataset.dta", clear
	drop if trt_exp==4 // Drop this experiment as there is another major wage policy less than six months prior. Leaves 20 experiments

	* Restrict to experiment window
	keep if inrange(etime,-6,5)
	gen own_gap=T

	* Program ------------------------------------------------------------------
			
	* Define event study programs: general, wage, employment, and combined figures
	cap program drop vmw_eventstudy
	program define vmw_eventstudy
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff,  title(" ") xline(-0.5) xline(-2.5) vrs("T_") yti(`ytitle') name(`expname')
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end	

	cap program drop vmw_eventstudy_wage
	program define vmw_eventstudy_wage
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff,  title(" ") xline(-0.5) xline(-2.5) vrs("T_") yti(`ytitle') name(`expname') 
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end	
	
	cap program drop vmw_eventstudy_wage_24
	program define vmw_eventstudy_wage_24
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff,  title(" ") xline(-0.5) xline(-8.5) vrs("T_") yti(`ytitle') name(`expname') 
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end		

	cap program drop vmw_eventstudy_emp
	program define vmw_eventstudy_emp
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff,  title(" ")  xline(-0.5) xline(-2.5) vrs("T_") yti(`ytitle') name(`expname') 
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end	

	cap program drop vmw_eventstudy_combined
	program define vmw_eventstudy_combined
		
		args outcome ytitle restriction expname controls 
				
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff, xti(" ") title(" ") xline(-0.5) xline(-2.5)  vrs("T_") yti(" ") name(`expname') 
		
	end	

	* Run ----------------------------------------------------------------------

	* Fig 3: Own-wage effect
	la var ln_avg_wage_exact "log average hourly wage"
	local varname: variable label ln_avg_wage_exact
	
	preserve
	keep if mw==15
	egen own_gap_std=std(own_gap)
	replace T=own_gap_std	
	
	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
	
	vmw_eventstudy_wage ln_avg_wage_exact "Effect on `varname'" "mw==15" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "" "fig4_own_wage_15"
	sum own_gap if e(sample)
	local avggap15 = round(`r(mean)',.01)
	local avggap15_pct=round(`r(mean)'*100,1)
	local T0_15=round(_b[T_0],.01)
	local norm_T0_15=round((exp(`avggap15'*`T0_15')-1)*100,1)
	PrintEst `avggap15' "avggap15"	"" "%" 4.2f "$figures_tables/text"
	PrintEst `avggap15_pct' "avggap15_pct"	"" "%" 4.0f "$figures_tables/text"
	PrintEst `T0_15' "T0_15"	"" "%" 4.2f "$figures_tables/text"
	PrintEst `norm_T0_15' "norm_T0_15"	"" "%" 4.0f "$figures_tables/text"
	restore 
	
	* Fig C1
	preserve
	keep if mw==15
	gen zerogap=(own_gap==0)
	collapse (mean) ln_avg_wage_exact, by(zerogap etime)
	keep if inrange(etime, -6,5)
	
	gen omit_wage=ln_avg_wage_exact
	replace omit_wage=. if etime!=-1
	egen mn_omit_wage=mean(omit_wage),by(zerogap)
	gen dln_wage= ln_avg_wage_exact-mn_omit_wage
	
	xtset zerogap etime	
	xtline dln_wage , overlay plot1(lcolor("$dnwblue")) plot2(lcolor("$dnwcrimson")) ///
	legend(order(1 "Gap > 0" 2 "Gap = 0") ring(0) pos(5)) ytitle(Log hourly wage) yline(0) xline(-0.5) ///
	xtitle("Event time in months")
	graph export "$figures_tables/appendix/figc1_zero_gap_15.pdf", replace  
	restore

	* Appendix Figure C2: Longer event window figure
	preserve
	* Merge with policy firm data
	use "$data/cb/stacked_policy_firm_dataset.dta", clear
	drop if trt_exp==4 // Drop this experiment as there is another major wage policy less than six months prior. Leaves 20 experiments
	keep if balanced_long==1
	
	* Restrict to experiment window
	keep if inrange(etime,-12,11)
		
	* Drop overlapping experiments
	keep if months_since_last_policy>=12 & months_until_next_policy>=12
	tab trt_exp
	
	* Use placebo gap measure
	egen avgplacebogap_std=std(avgplacebogap),by(trt_exp)
	replace T=avgplacebogap_std
	
	* Program ------------------------------------------------------------------
	
	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"

	la var ln_avg_wage_exact "log average hourly wage"
	local varname: variable label ln_avg_wage_exact
	vmw_eventstudy_wage_24 ln_avg_wage_exact "Effect on `varname'" "trt_exp>0 & balanced_long==1" "all" "i.cz##i.trt_exp i.etime##i.trt_exp" "appendix/" "figc2_wage_24_months_15"
	restore
	
	* All experiments
	preserve
	keep if trt_exp>0
	egen own_gap_std=std(own_gap)
	replace T=own_gap_std	
	
	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
	
	* Figs C3 and C4: Own-wage effect for all experiments and major experiments
	local varname: variable label ln_avg_wage_exact
	vmw_eventstudy_wage ln_avg_wage_exact "Effect on `varname'" "trt_exp>0" "all" "i.cz##i.trt_exp i.etime##i.trt_exp" "appendix/" "figc3_own_wage_all"
	restore
	
	* Major experiments
	preserve
	keep if share_affected>.3
	egen own_gap_std=std(own_gap)
	replace T=own_gap_std	
		
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"

	local varname: variable label ln_avg_wage_exact
	vmw_eventstudy_wage ln_avg_wage_exact "Effect on `varname'" "share_affected>.3" "major" "i.cz##i.trt_exp i.etime##i.trt_exp" "appendix/" "figc4_own_wage_major"
	restore

	keep if mw==15
	egen own_gap_std=std(own_gap)
	replace T=own_gap_std	
	
	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
	
	* Winsorize new hires variable due to outliers
	*hist yoy_ln_tot_new_hires_7_29 if mw==15
	bysort trt_exp: sum yoy_ln_tot_new_hires_7_29,d
	replace yoy_ln_tot_new_hires_7_29 = . if yoy_ln_tot_new_hires_7_29>`r(p90)' | yoy_ln_tot_new_hires_7_29<`r(p10)'
	
	* Label employment outcome variables
	la var ln_tot_emp_7_29 "log total employment <$30/hr"
	la var sep_rate_7_29 "separation rate <$30/hr"
	la var sep_to_nonpolicy_rate "rate of separations to non-policy companies"
	la var yoy_ln_tot_new_hires_7_29 "year-on-year dif. in log new hires <$30/hr"
	la var ln_tot_new_hires_7_29 "log new hires <$30/hr"
	la var ln_new_hires_nonpolicy "log new hires from nonpolicy"
	
	* Fig 4: Own-employment effect
	* Panel a: Log total employment under $30
	local varname: variable label ln_tot_emp_7_29
	vmw_eventstudy ln_tot_emp_7_29 "Effect on `varname'" "mw == 15" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "" "fig5a_ln_tot_emp_7_29_15"
	sum own_gap if e(sample)
	local avggap15 = round(`r(mean)',.01)
	local T0_emp_15=round(100*_b[T_0],1)
	local norm_T0_emp_15=round((exp(`avggap15'*round(_b[T_0],.01))-1)*100,1)
	PrintEst `T0_emp_15' "T0_emp_15"	"" "%" 4.0f "$figures_tables/text"
	PrintEst `norm_T0_emp_15' "norm_T0_emp_15"	"" "%" 4.0f "$figures_tables/text"
	
	* Panel a: Separation rate under $30
	local varname: variable label sep_rate_7_29
	vmw_eventstudy sep_rate_7_29 "Effect on `varname'" "mw==15 & outlier_retain==0" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "" "fig5b_sep_rate_7_29_15"

	* Panel a: Rate of separations to non-policy companies
	local varname: variable label sep_to_nonpolicy_rate
	vmw_eventstudy sep_to_nonpolicy_rate "Effect on `varname'" "mw == 15" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "" "fig5c_sep_to_nonpolicy_rate_15"

	* Panel a: Year-on-year difference in log new hires under $30
	local varname: variable label yoy_ln_tot_new_hires_7_29
	vmw_eventstudy yoy_ln_tot_new_hires_7_29 "Effect on `varname'" "mw == 15" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "" "fig5d_yoy_ln_tot_new_hires_7_29_15"

	* Panel a: Log new hires under $30
	local varname: variable label ln_tot_new_hires_7_29
	vmw_eventstudy ln_tot_new_hires_7_29 "Effect on `varname'" "mw == 15" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "" "fig5d_ln_tot_new_hires_7_29_15"

	* Panel a: Log new hires non-policy
	local varname: variable label ln_new_hires_nonpolicy
	vmw_eventstudy ln_new_hires_nonpolicy "Effect on `varname'" "mw == 15" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "" "fig5d_ln_new_hires_nonpolicy_15"
	
	* Figs C5 and C6: Own effects on monthly pay and gross total compensation
	la var ln_avg_monthly_pay_ytd "log average monthly base pay"
	local varname: variable label ln_avg_monthly_pay_ytd
	vmw_eventstudy_wage ln_avg_monthly_pay_ytd "Effect on `varname'" "mw == 15" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "appendix/" "figc5_own_monthly_base_pay_15"
	
	la var ln_avg_grs_total "log average monthly gross pay"
	local varname: variable label ln_avg_grs_total
	vmw_eventstudy_wage ln_avg_grs_total "Effect on `varname'" "mw == 15" "mw15" "i.cz##i.trt_exp i.etime##i.trt_exp" "appendix/" "figc6_own_monthly_gross_pay_15"

